package com.wechet.serveice.impl;

import com.wechet.common.OrderStatus;
import com.wechet.dto.OrderDto;
import com.wechet.exception.SellException;
import com.wechet.serveice.BuyerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by sc on 2017/9/23.
 */
@Service
@Slf4j
public class BuyerServiceImpl implements BuyerService{

    @Autowired
    private orderServiceImpl orderService;

    @Override
    public OrderDto findOrderOne(String openid, String orderid) {

        return checkOwner(openid,orderid);
    }

    @Override
    public OrderDto cancelOrder(String openid, String orderid) {
        OrderDto orderDto = orderService.findOne(orderid);
        if(orderDto==null){
            throw new SellException(OrderStatus.ORDER_NOT_EXIST);
        }
        if(!orderDto.getBuyerOpenid().equalsIgnoreCase(openid)){
            log.error("不是本人订单");
            throw new SellException("不是本人订单");
        }
        return orderService.cancel(orderDto);
    }



    public  OrderDto checkOwner(String openid, String orderid){
        OrderDto orderDto = orderService.findOne(orderid);
        if(orderDto==null){
            return null;
        }
        if(!orderDto.getBuyerOpenid().equalsIgnoreCase(openid)){
            log.error("不是本人订单");
            throw new SellException("不是本人订单");
        }
        return orderDto;
    }
}
